home *** CD-ROM | disk | FTP | other *** search
- // •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
- // Main.c
- //
- // July 31, 1996
- // By Ben Manuto
- //
- // © 1996 by Apple Computer, Inc., all rights reserved.
- //
- // Main entry point for our MacMsgTest tool.
- //
- // •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
-
-
-
-
- // •••••••••••••••••
- // Includes
-
- #include <Types.h>
- #include <LowMem.h>
- #include "Messaging.h"
- #include "MsgTest.h"
- #include "MsgUtil.h"
- #include <stdio.h>
-
-
- #define kEscKey 27 // The decimal value for the escape key.
-
- Boolean gSendCompCalled; // a global to let us know our completion routine was called.
- Boolean gRcvCompCalled; // a global to let us know our completion routine was called.
- Boolean gAckReceived;
- SInt32 gBytesReceived; // A global for the ack to tell us number of bytes received.
-
-
- // •••••••••••••••••
- // MyGetKey is a nasty little routine I copied that just reads the keyboard map to see if the 'ESC' key was pressed.
- // This reads and area of low-memory and will be incompatbile on future versions of the OS. This is bad! Don't do this!
-
- char MyGetKey(void)
- {
- if (*((char *) 0x17A) & 0x20)
- return kEscKey;
-
- return 0;
- }
-
-
- // •••••••••••••••••
-
- OSErr SendAck(SInt16 cmdBase, UInt32 bytesReceived)
- {
- MsgPBlkPtr ackMessage;
- OSErr error;
- char keyPressed = 0;
-
- ackMessage = NewMessage((cmdBase + kAckMsgType), bytesReceived, 0, 0);
-
- gSendCompCalled = false;
- error = SendMessage(ackMessage);
-
- printf("Sending Ack to PC.......");
- while ((gSendCompCalled == false) && (keyPressed != kEscKey)) { // Hang out until our completion routine is called.
- keyPressed = MyGetKey(); // If we hang here, offer a way out.
- }
-
- if (keyPressed == kEscKey) {
- printf("Waiting for ack to be sent was aborted!\n");
- return(-1);
- }
- else {
- printf("Ack sent to PC.\n");
- return(error);
- }
- }
-
-
-
- // •••••••••••••••••
-
- OSErr WaitForAck()
- {
- short bytes = kDefaultMsgSize;
- char keyPressed = 0;
-
- printf("Waiting for Ack from PC......");
- while ((gAckReceived == false) && (keyPressed != kEscKey)) { // Hang out until our completion routine is called.
- keyPressed = MyGetKey(); // If we hang here, offer a way out.
- }
-
- if (keyPressed == kEscKey) {
- printf("Waiting for ack was aborted!\n");
- return(-1);
- }
- else {
- printf("Ack received from PC.\n");
- if (gBytesReceived == bytes)
- printf("Message was sent successfully. All %d bytes were recevied.\n", bytes);
- else
- printf("The Message was not received properly. %d bytes were sent and %d were received.\n", bytes, gBytesReceived);
-
- return(noErr);
- }
- }
-
-
-
- // •••••••••••••••••
-
- main()
- {
- MsgPBlkPtr dataMessage, recMsg;
- MsgRecElemPtr theMsgRec;
- SInt16 i, cmdBase;
- OSErr error;
- char keyPressed = 0;
-
- error = InitializeMsgSystem(); // Initialize the messaging system.
- if (error) {
- printf("Could not initialize the messaging system. Err = %d\n\n", error);
- return;
- }
-
- gBytesReceived = 0; // Init this global.
-
- cmdBase = RegisterMessage(kMsgCommandType, kDefaultMsgTypes); // Register a message command type and anumber of types.
- if (cmdBase < 0) goto Exit;
- printf("Message registered. CmdBase = %d\n", cmdBase);
-
- recMsg = NewMessage(cmdBase, kDefaultSelector, // Create a new receiver msgPBlk.
- kDefaultMsgSize, kDefaultMsgSize);
-
- theMsgRec = NewMsgRecElem(cmdBase, kDefaultMsgTypes, (UInt32) recMsg); // Create a new MsgRecElem.
- printf("Message Receive Element Created.\n");
-
- error = InstallMsgHandler(theMsgRec); // Install a message handler.
- printf("Message Handler Installed. Error = %d\n", error);
-
- dataMessage = NewMessage(cmdBase, kDefaultSelector, // Create a new message PBlk
- kDefaultMsgSize, kDefaultMsgSize);
- if (dataMessage == 0L) {
- printf("Unable to create message....aborting.\n");
- goto Exit;
- }
-
- gSendCompCalled = false; // Mark that the send completion routine has not been called.
- gRcvCompCalled = false; // Mark that the receive completion routine has not been called.
- gAckReceived = false; // Mark that an acknowledge has not been received from PC.
-
- error = SendMessage(dataMessage); // Send our message.
- printf("Sending Message. Error = %d\n", error);
-
- printf("Waiting for Send Completion routine to be called......");
- while ((gSendCompCalled == false) && (keyPressed != kEscKey)) { // Hang out until our completion routine is called.
- keyPressed = MyGetKey(); // If we hang here, offer a way out.
- }
- if (keyPressed == kEscKey) goto Exit; // If we had to bail, clean up and bail.
- printf("Send Completion routine called\n");
-
- error = WaitForAck(); // Wait for Ack message from PC so we know data was sent.
- if (error) goto Exit;
-
- printf("Waiting for Receive Completion routine to be called......");
- while ((gRcvCompCalled == false) && (keyPressed != kEscKey)) { // Hang out until our completion routine is called.
- keyPressed = MyGetKey(); // If we hang here, offer a way out.
- }
-
- if (keyPressed == kEscKey) goto Exit; // If we had to bail, clean up and bail.
- printf("Receive Completion routine called\n");
-
- printf("\n\nMessage Data:\n");
- printf("\nParam1 = $%08lX Param2 = $%08LX\n",
- recMsg->msgParam1, recMsg->msgParam2);
-
- (UInt32) (recMsg->msgBuffer) -= recMsg->msgActCount; // Set the ptr back to the beginning of the buffer.
-
- for (i = 0; i < recMsg->msgActCount; i++) { // Display the data we received.
- printf("$%02X ", ((UInt8*)(recMsg->msgBuffer))[i]);
- if (((i+1) % 16) == 0) printf("\n");
- }
-
- error = SendAck(cmdBase, recMsg->msgActCount); // Send message to PC Acknowledging data received.
-
- Exit:
- RemoveMsgHandler(theMsgRec);
- printf("\nMessage Handler removed.\n");
-
- DeleteMsgRecElem(theMsgRec);
- DeleteMessage(dataMessage);
- }